package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.CalendarCategory;
import org.hgvc.webapp.ets.bo.CalendarCategoryDtl;
import org.hgvc.webapp.ets.bo.CalendarDtl;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.PartnerConstant;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;

import com.j6.framework.util.DateUtil;


public class AdminCalenderDtlMasterBean extends AbstractBean {

	public static final String BEAN_NAME="adminCalenderDtlMasterBean";
	private CalendarDtl calendarDtl;
	private CalendarCategory cat;
	private PartnerService service;
	private List<SelectItem> typeList;
	private List<SelectItem> seasonList;
	private List<SelectItem> calendarList;
	private List<CalendarDtl> calList;
	private List<CalendarCategoryDtl> calCategoryList;
	public static final String NAV_EDIT_ACTION="adminPartnerProductCalDtlEdit";
	public static final String NAV_CREATE_ACTION="adminPartnerProductCalDtlCreate";
	public static final String NAV_LIST_ACTION="adminPartnerProductCalDtlList";
	public String dateType = PartnerConstant.PARTNER_CAL_TYPE_DATE;
	private boolean flag;
	private String select;
	private CalendarCategoryDtl calendarCategoryDtl;
	private Date toDate;
	
	public Date getToDate() {
		return toDate;
	}
	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}
	public CalendarDtl getCalendarDtl() {
		return calendarDtl;
	}
	public void setCalendarDtl(CalendarDtl calendarDtl) {
		this.calendarDtl = calendarDtl;
	}
	public CalendarCategory getCat() {
		return cat;
	}
	public void setCat(CalendarCategory cat) {
		this.cat = cat;
	}
	public void setService(PartnerService service) {
		this.service = service;
	}
	public String getDateType() {
		return dateType;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public String getSelect() {
		return select;
	}
	/*------------------------------------------------------------------*/
	
	
	public void setSelect(String select) {
		if(select.equalsIgnoreCase(PartnerConstant.PARTNER_CAL_TYPE_DAY)){
			flag = true;
		}else{
			flag = false;
		}
		this.select = select;
	}
	
	public String init(CalendarCategory cat){
		this.cat = cat;
		return NAV_LIST_ACTION;
	}
	
	public List<SelectItem> getTypeList() {
		
		typeList = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem(PartnerConstant.PARTNER_CAL_TYPE_DATE
				,SpringUtils.getMessage(PartnerConstant.PARTNER_CAL_TYPE_DATE, new Object[]{}));
		SelectItem item2 = new SelectItem(PartnerConstant.PARTNER_CAL_TYPE_DAY
				,SpringUtils.getMessage(PartnerConstant.PARTNER_CAL_TYPE_DAY, new Object[]{}));
		typeList.add(item1);
		typeList.add(item2);
		return typeList;
	}
	public List<SelectItem> getSeasonList() {
		
		seasonList = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem(PartnerConstant.PARTNER_PRODUCT_SUPER_PEAK_SEASON
				,SpringUtils.getMessage(PartnerConstant.PARTNER_PRODUCT_SUPER_PEAK_SEASON, new Object[]{}));
		SelectItem item2 = new SelectItem(PartnerConstant.PARTNER_PRODUCT_PEAK_SEASON
				,SpringUtils.getMessage(PartnerConstant.PARTNER_PRODUCT_PEAK_SEASON, new Object[]{}));
		/*SelectItem item3 = new SelectItem(PartnerConstant.PARTNER_PRODUCT_NORMAL_SEASON
				,SpringUtils.getMessage(PartnerConstant.PARTNER_PRODUCT_NORMAL_SEASON, new Object[]{}));*/
		seasonList.add(item1);
		seasonList.add(item2);
		//seasonList.add(item3);
		return seasonList;
	}
	public List<CalendarDtl> getCalList() {
		
		try{
			calList = service.getCalendarDtlList(cat.getCalendarCatId());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return calList;
	}

	public List<SelectItem> getCalendarList() {
		calendarList = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem(CommonConstant.CAL_SUN
				,SpringUtils.getMessage(CommonConstant.CAL_SUN, new Object[]{}));
		SelectItem item2 = new SelectItem(CommonConstant.CAL_MON
				,SpringUtils.getMessage(CommonConstant.CAL_MON, new Object[]{}));
		SelectItem item3 = new SelectItem(CommonConstant.CAL_TUES
				,SpringUtils.getMessage(CommonConstant.CAL_TUES, new Object[]{}));
		SelectItem item4 = new SelectItem(CommonConstant.CAL_WED
				,SpringUtils.getMessage(CommonConstant.CAL_WED, new Object[]{}));
		SelectItem item5 = new SelectItem(CommonConstant.CAL_THURS
				,SpringUtils.getMessage(CommonConstant.CAL_THURS, new Object[]{}));
		SelectItem item6 = new SelectItem(CommonConstant.CAL_FRI
				,SpringUtils.getMessage(CommonConstant.CAL_FRI, new Object[]{}));
		SelectItem item7 = new SelectItem(CommonConstant.CAL_SAT
				,SpringUtils.getMessage(CommonConstant.CAL_SAT, new Object[]{}));
		
		calendarList.add(item1);
		calendarList.add(item2);
		calendarList.add(item3);
		calendarList.add(item4);
		calendarList.add(item5);
		calendarList.add(item6);
		calendarList.add(item7);
		return calendarList;
	}
	
	public String navCreateCategoryAction(){
		calendarCategoryDtl = new CalendarCategoryDtl();
		return "adminPartnerProductCalCategoryDtlCreate";
	}
	public String createCategoryAction(){
		try{
			calendarCategoryDtl.setCreatedBy(getThisUser());
			calendarCategoryDtl.setCreatedDate(new Date());
			calendarCategoryDtl.setCalCat(cat);
			calendarCategoryDtl.setIsDeleted(CommonConstant.NOT_DELETED);
			service.saveCalendarCategoryDtl(calendarCategoryDtl );
			
			return NAV_LIST_ACTION;
		}catch(Exception be){
			be.printStackTrace();
			initErrorBean(be.getMessage());
		}
		
		return "adminPartnerProductCalCategoryDtlCreate";
	}
	public String navCreateAction(){
		calendarDtl = new CalendarDtl();
		calendarDtl.setType(PartnerConstant.PARTNER_CAL_TYPE_DAY);
		select = calendarDtl.getType();
		flag = true;
		return NAV_CREATE_ACTION;
	}
	
	public String createAction(){
		try{
			calendarDtl.setType(select);
			 
			
			service.createCalendarDtl(cat, getThisUser(),calendarDtl, toDate);
			
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_CREATE_ACTION;
	}

	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("catId");
		String id = (String)param.getValue();

		for(CalendarDtl c:calList){
			if(c.getCalendarDtlId().equalsIgnoreCase(id)){
				calendarDtl = c;
				setSelect(calendarDtl.getType());
				break;
			}
		}
	}
	public void editCategoryActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("catId");
		String id = (String)param.getValue();

		for(CalendarDtl c:calList){
			if(c.getCalendarDtlId().equalsIgnoreCase(id)){
				calendarDtl = c;
				setSelect(calendarDtl.getType());
				break;
			}
		}
	}
	
	public String navCategoryEditAction(){
		return "adminPartnerProductCalCategoryDtlEdit";
	}
	
	public String navEditAction(){
		return NAV_EDIT_ACTION;
	}

	public String editAction(){
		try{
			calendarDtl.setType(select);
			service.updateCalendarDtl(cat, getThisUser(),calendarDtl);
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_EDIT_ACTION;
	}
	
	public String deleteCategoryAction(){
		try{
			service.deleteCalendarCategoryDtl(calCategoryList, getThisUser());
			return NAV_LIST_ACTION;
		}catch(Exception be){
			be.printStackTrace();
			initErrorBean(be.getMessage());
		}
		return NAV_LIST_ACTION;
	}
	
	public String deleteAction(){
		try{
			service.deleteCalendarDtl(calList, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_LIST_ACTION;
	}
	
	public String navListAction(){
		return NAV_LIST_ACTION;
	}
	public List<CalendarCategoryDtl> getCalCategoryList() {
		try{
			calCategoryList = service.getCalendarCategoryDtls(cat.getCalendarCatId());
		}catch(Exception be){
			be.printStackTrace();
			initErrorBean(be.getMessage());
		}
		return calCategoryList;
	}
	public void setCalCategoryList(List<CalendarCategoryDtl> calCategoryList) {
		this.calCategoryList = calCategoryList;
	}
	public CalendarCategoryDtl getCalendarCategoryDtl() {
		return calendarCategoryDtl;
	}
	public void setCalendarCategoryDtl(CalendarCategoryDtl calendarCategoryDtl) {
		this.calendarCategoryDtl = calendarCategoryDtl;
	}
	 
 
	
}
